// index.js
// const app = getApp()
import request from "../../utils/request"
Page({
  onUpdateData(e) {
    // 在这里更新父组件的数据
    console.log(e)
    this.setData({ language: e.detail.newData });
    wx.setStorage({
      key: 'language',
      data: this.data.language,
      success: function (res) {
      
      }
    });
  },
  onLanguageTap() {
    const that = this;
    wx.showActionSheet({
        itemList: ['English', '中文','繁体'],
        success(res) {
            if (res.tapIndex === 0) {
                that.setData({ language: 'en' });
            } else if (res.tapIndex === 1) {
                that.setData({ language: 'zh' });
            }else{
              that.setData({language:'tr'})
            }
            wx.setStorage({
              key: 'language',
              data: that.data.language,
              success: function (res) {
              }
            });
        }
    });
  },
  data: {
    title: '福州软件职业技术学院 - 会议活动',
    type1:'',
    type3:'',
    all:'',
    filteredItems:'',
    filteredItems1:'',
    filteredItems2:'',
    filteredItems3:'',
    navList: [{
        title: '即将开始',
        length:''
      },
      {
        title: '进行中',
        length:''
      },
      {
        title: '已结束',
        length:''
      },
    ],
    // 被选中的下标
    chosed: 0,
    start_total: 0,
    press_total: 2,
    finish_total: 8,
    all_total: 10,
    meetingList: [],
    indexId: '',
    status: 'start',
    id:'',
    type:''
  },
  async queryInfo(id) {
    try {
      const res1 = await request({
        url: "/queryinfo",
        data: {
          id: id,
        }
      });
      this.setData({
        type: res1.data.type,
      });
      return res1
    } catch (error) {
      console.error('请求失败:', error);
    }
  },
  query(e) {
    // 判断item的类型，从而跳转不同的页面
    console.log(e.currentTarget.dataset.index,"hhhh")
    
    const indexId = e.currentTarget.dataset.index.id
    const type = e.currentTarget.dataset.index.type
    console.log(type,"ccs")
    this.queryInfo(indexId).then((res1) => {
      wx.setStorageSync('is_seat_phone', res1.data.is_seat_phone)
      wx.setStorageSync('actId', indexId)
      this.setData({
        indexId: indexId
      })
      console.log(this.data.indexId)
      if(this.data.status ==='finish'){
        wx.showModal({
          title: '提示', // 对话框标题
          content: '活动已结束', // 对话框内容
          confirmText: '确定', // 确定按钮的文字，默认为确定
          showCancel: false,//是否显示取消按钮
          confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为绿色
        })

      }else{
      if(type == 2){
         wx.navigateTo({
        url: '/pages/member/member?param1=' + JSON.stringify(this.data.indexId)
      })
      }else if(type==1){
        wx.navigateTo({
        url: '/pages/activity/activity?param1=' + JSON.stringify(this.data.indexId)
      })
    }else if(type == 3){
      wx.navigateTo({
        url: '/pages/dannerdetail1/dannerdetail1?param1=' + JSON.stringify(this.data.indexId)
      })
    }else if(type == 4){
      wx.navigateTo({
        url: '/pages/meetingsummary/meetingsummary?param1='+JSON.stringify(this.data.indexId),
      })
    }
    }
    })
    
  },
  async navChange(e) {
    // 点击切换导航栏函数

    //todo:切换样式  更改index,以便切换主体内容
    //通过更给chosed来更改样式和数据源
    let index = e.currentTarget.dataset.index;
    this.setData({
      chosed: index,
    })
    if (index) {
      if (index == 1) {
        this.setData({
          status: 'progress',
        })

      } else if (index == 2) {
        this.setData({
          status: 'finish',
        })
      }
    } else {
      this.setData({
        status: 'start',
      })
    }
    const status1 = this.data.status
    const res2 = await request({
      url: "/getquerylist",
      data: {
        unit_id: this.data.id,
        type: this.data.type1,
        status: status1
      }
    })
    
    //  const dataLength = res2.data.length
    if(this.data.all='all'){
      this.setData({
        filteredItems : res2.data
      })

    }
    else{this.setData({
      filteredItems : res2.data.filter(item => item.type === this.data.type3)
    })
       }

    
     
      const navListCopy = this.data.navList
      navListCopy[index].length = this.data.filteredItems.length



    this.setData({
      meetingList: this.data.filteredItems,
      navList:navListCopy
    })
    wx.setStorageSync('navList',JSON.stringify(this.data.navList))
  },
  async onLoad(e) {

  var pages = getCurrentPages();
// 获取当前页面
var currentPage = pages[pages.length - 1];
// 获取当前页面的路由（即 URL）
var route = currentPage.route;
// 获取当前页面的参数
var options = currentPage.options;
console.log(options)


if(options.type === "meeting"){
  this.setData({
    type3:1
  })
}else if(options.type === "banquet"){
  this.setData({
    type3:2
  })
}else if(options.type === "meeting_3"){
  this.setData({
  type3:3
  })
}else if(options.type === "meeting_4"){
  this.setData({
    type3:4
  })
}else if(options.type === "all"){
  this.setData({
    all : 'all'
  })
}
if(options.type=='all'){
  this.setData({
    id: options.id,
    type1: 'meeting',
  })
}else{
this.setData({
  id: options.id,
  type1: options.type,
})
}

  

      // 调用接口
      const res1 = await request({
        url: "/querylist",
        data: {
          unit_id: options.id,
          type: this.data.type1
        }
      })
      this.setData({
        title: res1.data.type_name + "_" + res1.data.unit_name,
        all_total: res1.data.all_total,
        // finish_total: res1.data.finish_total,
        // press_total: res1.data.press_total,
        // start_total: res1.data.start_total,
      })
      //赋值长度
      
      // start（即将开始）progress（进行中）finish（已结束

      
  
      const res2 = await request({
        url: "/getquerylist",
        data: {
          unit_id: options.id,
          type: this.data.type1,
          status: "start"
        }
      })
      if(this.data.all='all'){
        this.setData({
          filteredItems1 : res2.data
        })
        
        
      }
      else{ this.setData({
        filteredItems1 : res2.data.filter(item => item.type === this.data.type3)
      })
        }

 

      const res3 = await request({
        url: "/getquerylist",
        data: {
          unit_id: options.id,
          type: this.data.type1,
          status: "progress"
        }
      })
      if(this.data.all='all'){
        this.setData({
          filteredItems2 : res3.data
        })
        
      }
      else{ 
        this.setData({ 
          filteredItems2 : res3.data.filter(item => item.type === this.data.type3)
        })
         }
     
      const res4 = await request({
        url: "/getquerylist",
        data: {
          unit_id: options.id,
          type: this.data.type1,
          status: "finish"
          
        }
      })
      if(this.data.all='all'){
        this.setData({
          filteredItems3 : res4.data
        })
       
      }
      else{
        this.setData({
          filteredItems3 : res4.data.filter(item => item.type === this.data.type3)

        }) 
        }

      

      this.setData({
        'navList[2].length': this.data.filteredItems3.length,
        'navList[1].length': this.data.filteredItems2.length,
        'navList[0].length': this.data.filteredItems1.length,

      })
      console.log(this.data.filteredItems,"1")
      console.log(this.data.filteredItems1,"2")
      console.log(this.data.filteredItems2,"3")
      console.log(this.data.filteredItems3,"4")

  
      this.setData({
        // navList:navListCopy,
        meetingList: this.data.filteredItems1
      }) 
      const app = getApp();
      this.setData({ gridList: app.globalData.lang });
      wx.getStorage({
        key: 'language',
        success:(res)=> {
         this.setData({
           language:res.data
         })
        }
      });
    }
    
});